﻿using System;
using System.IO;
using System.Net;
using System.Security;
using System.Security.Permissions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Diagnostics;
using MovieInfo;

namespace MovieTagger
{
	public partial class App: System.Windows.Application
	{
        /// <summary>
        /// This variable holds the main application window.
        /// </summary>
        MainWindow mW = new MainWindow();

        /// <summary>
        /// This is the main entry point for the application
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            ///first make sure that the application isn't already running
            ///

            Process[] procs = Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName);

            if (procs.Length > 1)
            {
                //another copy of the application is already running
                //TODO: pass the params to it
                Application.Current.Shutdown();


                for (int i = 0; i < procs.Length; i++)
                {
                    MessageHelper msg = new MessageHelper();

                    //we don't want to send a message to our current process
                    if (procs[i] != Process.GetCurrentProcess())
                    {
                        IntPtr hWnd = procs[i].MainWindowHandle;

                        foreach (string filename in e.Args)
                            msg.sendWindowsStringMessage((int)hWnd, 0, filename);
                    }
                }
            }




            mW.Closed += new EventHandler(mW_Closed);
            //Show the main window
            mW.Show();

            foreach (string file in e.Args)
            {
                mW.addFile(file);
            }
        }


        /// <summary>
        /// Event handler that is called just before the main window is closed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void mW_Closed(object sender, EventArgs e)
        {
            ///TODO: all of the cleanup code as the application is about to exit


            //Once the window is closed, shutdown the application
            Application.Current.Shutdown();
        }
	}
}
